/**
 * 事件处理系统
 */
export class EventSystem {
  private readonly canvas: HTMLCanvasElement
  private readonly hitRegions: Map<string, { x: number, y: number, width: number, height: number }>

  constructor (canvas: HTMLCanvasElement) {
    this.canvas = canvas
    this.hitRegions = new Map()

    canvas.addEventListener('click', this.handleClick.bind(this))
    // 其他事件监听...
  }

  registerHitRegion (id: string, rect: DOMRect): void {
    this.hitRegions.set(id, rect)
  }

  private handleClick (event: MouseEvent): void {
    const rect = this.canvas.getBoundingClientRect()
    const x = event.clientX - rect.left
    const y = event.clientY - rect.top

    for (const [id, region] of this.hitRegions.entries()) {
      if (
        x >= region.x && x <= region.x + region.width &&
        y >= region.y && y <= region.y + region.height
      ) {
        // TODO 事件抛出
        // this.emit('cellClick', { id, event })
        return
      }
    }
  }
}
